package;
import fl.controls.Button;
import fl.controls.List;
import fl.controls.TextArea;
import fl.controls.TextInput;
import fl.data.DataProvider;

import flash.Lib;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.Responder;

class Main extends MovieClip
{
	static var app:String='/space3D';
	static var path:String;
	var nc:NetConnection;
	var output : fl.controls.TextArea;
	var btn:Button;
	var login:Login;
	var game:Game;
	
	public static function main()
	{
		Lib.current.stage.scaleMode=StageScaleMode.NO_SCALE;
		Lib.current.stage.align=StageAlign.TOP_LEFT;
		var m = new Main();
		Lib.current.addChild(m);
	}
	public function new()
	{
		super();
		
		nc = new NetConnection();
		nc.client=this;
		nc.objectEncoding = 0;
		nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
		
		btn = new Button();
		btn.label="Connect";
		btn.addEventListener(MouseEvent.CLICK, onBtnClick);
		addChild(btn);
		
		output = new fl.controls.TextArea();
		output.tabChildren=false;
		output.mouseChildren=false;
		output.mouseEnabled=false;
		output.height = 125;
		addChild(output);
		
		positionMiddle(btn);
		addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
	}
	function onAddedToStage(event:Event):Void
	{
		removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		stage.addEventListener(Event.RESIZE, onStageResize);
		onStageResize(null);
		path = "rtmp://"+getUri();
	}
	function onStageResize(event:Event):Void
	{
		if(btn!=null && btn.parent!=null)
			positionMiddle(btn);
			
		if(login!=null && login.parent!=null)
			positionMiddle(login);
			
		if(game!=null && game.parent!=null)
			positionMiddle(game);
			
		output.width = stage.stageWidth;
	}
	function onBtnClick(event:MouseEvent):Void
	{
		nc.connect(path);//nc.connect(path, {name:"jan"});
	}
	function onNetStatus(event:NetStatusEvent):Void
	{
		log("code:" + event.info.code);
		log("description:" + event.info.description);
		if(event.info.code =='NetConnection.Connect.Success')
		{
			removeChild(btn);
			login = new Login(nc, log);
			login.addEventListener("login", onLogin);
			addChild(login);
			positionMiddle(login);
		}
	}
	function onLogin(event:Event):Void
	{
		login.removeEventListener("login", onLogin);
		removeChild(login);
		
		game = new Game(nc, login.getUserName(), log);
		addChild(game);
		//positionMiddle(game);
	}
	function log(msg:Dynamic)
	{
		output.text+= formatDate(Date.now()) + " " + msg+"\n";
		output.textField.scrollV = output.textField.maxScrollV;
	}
	function formatDate(d:Date):String
	{
		var h:Dynamic = d.getHours();
		if(h<10) h="0"+h;
		var m:Dynamic = d.getMinutes();
		if(m<10) m="0"+m;
		return "[" + h + ":" + m + "]";
	}
	function positionMiddle(obj:flash.display.DisplayObject)
	{
		obj.x = (Lib.current.stage.stageWidth-obj.width)/2;
		obj.y = (Lib.current.stage.stageHeight-obj.height)/2;
	}
	function getUri():String
	{
		var parameters:Dynamic = flash.Lib.current.loaderInfo.parameters;
		//trace(parameters);
		var u = Reflect.field(parameters, "uri");
		if(u!=null)
			return u;
		var h = Reflect.field(parameters, "host");
		var p = Reflect.field(parameters, "port");
		var a = Reflect.field(parameters, "app");
		return (h==null?"localhost":h) + (p==null?":1935":":"+p) + (a==null?app:a);
	}
}